package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import cn.finalteam.okhttpfinal.HttpRequest;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.bean.BookComment;
import me.shurufa.bean.UserInfoEntity;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.model.BookCommentResponse;
import me.shurufa.net.API;
import me.shurufa.net.RequestParams;
import me.shurufa.net.RequestServerTask;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.CreateBookCommentResp;
import me.shurufa.utils.Constants;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Utils;

/**
 * 作者 : andy
 * 日期 : 16/3/24 09:23
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 回复评论
 */
public class ModifyBookCommentActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.et_content)
    EditText et_content;

    private String mCommentId;
    private String mContent;
    private int mPosition;  // 上一个列表的所修改的position
    private Handler mHandler = new Handler();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_send_book_comment;
        super.onCreate(savedInstanceState);
        try {
            parseArguments();
            initUI();
        } catch (Exception e) {
            e.printStackTrace();
            Utils.showToast(R.string.sys_err);
        }
    }

    private void initUI() {
        titleTextView.setText("修改书评");
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getString(R.string.complete));
        rightText.setOnClickListener(this);
        et_content.setText(mContent);
        Utils.moveCursorToLast(et_content);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Utils.showSoftkeyboard(ModifyBookCommentActivity.this, et_content);
            }
        }, 400);
    }

    public static void initArguments(Intent in, int position, String commentId, String content) {
        in.putExtra(Constants.ARG_MODIFY_POSITION, position);
        in.putExtra(Constants.ARG_COMMENT_ID, commentId);
        in.putExtra(Constants.ARG_COMMENT_CONTENT, content);
    }

    private void parseArguments() {
        if (getIntent() != null) {
            mPosition = getIntent().getIntExtra(Constants.ARG_MODIFY_POSITION, 0);
            mContent = getIntent().getStringExtra(Constants.ARG_COMMENT_CONTENT);
            mCommentId = getIntent().getStringExtra(Constants.ARG_COMMENT_ID);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                final String content = et_content.getText().toString().trim();
                if (!TextUtils.isEmpty(content) && content.length() > 6) {
                    reqModifyBookComment(content);
                } else if (TextUtils.isEmpty(content)) {
                    Utils.showToast(R.string.content_is_empty);
                } else if (content.length() < 10){
                    Utils.showToast("内容不可少于10字");
                } else if (content.length() > 3000) {
                    Utils.showToast("内容不可超过3000字");
                } else {
                    Utils.showToast("内容字数限制10-3000字");
                }
                break;
        }
    }

    /**
     * 请求修改
     * @param content
     */
    private void reqModifyBookComment(final String content) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", mCommentId);
        params.addFormDataPart("content", content);

        HttpRequest.post(API.EDIT_BOOK_COMMENT, params, new HttpCallback<CreateBookCommentResp>(this) {
            @Override
            public void onRespSuccess(CreateBookCommentResp resp) {
                try {
                    Utils.showToast(getString(R.string.commit_success));
                    et_content.setText("");

                    Intent in = new Intent(Constants.EVENT_MODIFY_BOOK_COMMENT);
                    in.putExtra(Constants.ARG_COMMENT_CONTENT, content);
                    in.putExtra(Constants.ARG_MODIFY_POSITION, mPosition);
                    EventBus.getDefault().post(in);

                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 100);
                } catch (Exception e) {
                }
            }

            @Override
            public void onRespFailure(CreateBookCommentResp resp) {

            }

            @Override
            public void onStart() {
                super.onStart();
                Utils.closeInputMethod(ModifyBookCommentActivity.this, et_content);
            }
        });
    }
}
